﻿using System.Collections.Generic;
using System.Linq;
using Client.ViewModels;
using Contracts.Entities;

namespace Client.Commands
{
    public class RemoveActorCommand : BaseCommand
    {

        public override bool CanExecute(object parameter)
        {
            return parameter as MovieElement != null &&  MainViewModel.Default.ActiveWorkspace.MovieItem.Actors != null;
        }

        public override void Execute(object parameter)
        {
            var movie = MainViewModel.Default.ActiveWorkspace.MovieItem;
            var element = (parameter as MovieElement);
            if (movie == null)
                return;

            var actors = new List<MovieElement>(movie.Actors);
            var actor = actors.SingleOrDefault(m => m.Name == element.Name);
                 
            actors.Remove(actor);                  
            movie.Actors = actors;

            MainViewModel.Default.ActiveWorkspace.MovieItem = movie;
       }
    }
}
